'''
Created on 06/07/2012

@author: mekoda
'''
from main.cpu import CPU
from main.dataStructures import ProcessesQueue
from main.ioDevs import IO, Printer
from main.kernel import Kernel
from main.longTermSchedulers import LongTermScheduler, \
    LongTermScheduler_withBanker
from main.memory import MMU_ContinuousAssignment, BestAdjustment, PhysicalMemory, \
    Disk
from main.process import PCB, Instruction, Process, IOInstruction, ProcessStatus
from main.resourceAssignment import Banker
from main.shortTermSchedulers import Scheduler_ShortestJobFirst_E
import unittest

class TestBanquero(unittest.TestCase):
    
    def setUp(self):
        self.memory = PhysicalMemory(800,BestAdjustment())
        self.mmu = MMU_ContinuousAssignment()
        self.disk = Disk()
        self.pcb1 =  PCB(3)
        self.pcb2 =  PCB(6)
        self.pcb3 =  PCB(7)
        self.pcb4 =  PCB(5)
        self.instruction1 =  Instruction(ProcessStatus().CPU,2)
        self.instruction2 =  Instruction(ProcessStatus().CPU,2)
        self.instruction3 =  Instruction(ProcessStatus().CPU,2)
        self.instruction4 =  IOInstruction(2,'Printer')         #Tiempo total del proceso1: 8
        self.instruction5 =  Instruction(ProcessStatus().CPU,3)
        self.instruction6 =  IOInstruction(3,'Printer')
        self.instruction7 =  Instruction(ProcessStatus().CPU,5) #Tiempo total del proceso2: 11
        self.instruction8 =  Instruction(ProcessStatus().CPU,6)
        self.instruction9 =  IOInstruction(4,'Printer')
        self.instruction10=  Instruction(ProcessStatus().CPU,2) #Tiempo total del proceso1: 12
        self.instruction11=  Instruction(ProcessStatus().CPU,1)
        self.instruction12=  Instruction(ProcessStatus().CPU,1) #Tiempo total del proceso1: 2
        #------------------------------------------#
        self.process1 = Process(self.pcb1)
        self.process1.addInstruction(self.instruction1)
        self.process1.addInstruction(self.instruction2)
        self.process1.addInstruction(self.instruction3)
        self.process1.addInstruction(self.instruction4)
        #---------------------------------------------#
        self.process2 = Process(self.pcb2)
        self.process2.addInstruction(self.instruction5)
        self.process2.addInstruction(self.instruction6)
        self.process2.addInstruction(self.instruction7)
        #---------------------------------------------#
        self.process3 = Process(self.pcb3)
        self.process3.addInstruction(self.instruction8)
        self.process3.addInstruction(self.instruction9)
        self.process3.addInstruction(self.instruction10)
        #---------------------------------------------#
        self.process4 = Process(self.pcb4)
        self.process4.addInstruction(self.instruction11)
        self.process4.addInstruction(self.instruction12)
        #----------------------------------------------#
        self.process = ProcessesQueue()
        self.process.append(self.process1)
        self.readyQueue = ProcessesQueue()
        self.io = IO()
        self.cpu = CPU(self.readyQueue,self.io,self.mmu,self.memory)
        self.planner = Scheduler_ShortestJobFirst_E(self.readyQueue,self.cpu,self.io)
        self.kernel = Kernel("self.longPlanner",self.planner,self.cpu,self.process,self.readyQueue,ProcessesQueue(),self.memory,self.disk)
        self.cpu.addKernelToCpu(self.kernel)
        devicesPatch = []
        devicesPatch.append(Printer(1,self.kernel,self.io.processesUsingADevice))
        devicesPatch.append(Printer(2,self.kernel,self.io.processesUsingADevice))
        self.io.devices = devicesPatch
        self.longPlanner = LongTermScheduler_withBanker(self.process, self.readyQueue, self.kernel)  
        self.kernel.longTermScheduler = self.longPlanner


    def test_putAProcessInReadyProcessQueueShouldEnter(self):
        self.longPlanner.dispatchAProcess(self.memory,self.disk)               #El PLP lleva 3 procesos a la cola de listos                                      #El PCP lleva uno al Cpu(al mas corto)
        self.assertEquals(self.longPlanner.readyProcessesQueue.length(), 1)    #Hay tres procesos en la cola de listos, incluso el proceso que hace que el PCP avise que hay que expropiar
        self.assertEquals(self.longPlanner.arrivingProcessesQueue.length(), 0) #Ningun proceso queda en la cola de llegada

suite = unittest.TestLoader().loadTestsFromTestCase(TestBanquero)
unittest.TextTestRunner(verbosity=2).run(suite)      